// Name：P6_9.c
// Purpose：计算还贷金额
// Author：wjy
// From：《C语言程序设计：现代方法》（第二版）第六章第9题
// Date：2021.1.14

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    float amount;
    float rate;
    float payment;
    int num;

    printf("Enter amount of loan: ");
    scanf("%f", &amount);
    printf("Enter interest rate: ");
    scanf("%f", &rate);
    printf("Enter monthly payment: ");
    scanf("%f", &payment);
    printf("Enter your repayment: ");
    scanf("%d", &num);
    rate = rate / 1200.0f;

    for (int i = 0; i < num; i++)
    {
        if (i == 0)
        {
            printf("Balance remaining after first payment: $%.2f\n", amount * (1.0f + rate) - payment);
        }
        if (i == 1)
        {
            printf("Balance remaining after second payment: $%.2f\n", amount * (1.0f + rate) - payment);
        }
        if (i == 2)
        {
            printf("Balance remaining after third payment: $%.2f\n", amount * (1.0f + rate) - payment);
        }
        if (i > 2)
        {
            printf("Balance remaining after %dth payment: $%.2f\n", i + 1, amount * (1.0f + rate) - payment);
        }
        amount = amount * (1.0f + rate) - payment;
        if (amount <= 0)
        {
            printf("Your loan has been paid off\n");
            break;
        }
    }
    system("pause");
    return 0;
}